package com.westa.smallshop.cart;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.daimajia.swipe.SwipeLayout;
import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseListAdapter;
import com.westa.smallshop.data.GoodsItem;
import com.westa.smallshop.util.HttpUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by cy on 10/11/16.
 */

public class CartAdapter extends BaseListAdapter<CartAdapter.ViewHolder, GoodsItem> {


    private CartFragment mCartFragment;

    private LayoutInflater mLayoutInflater;

    CartAdapter(CartFragment cartFragment) {
        mCartFragment = cartFragment;
        mLayoutInflater = mCartFragment.getActivity().getLayoutInflater();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.item_goods_in_cart, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        GoodsItem goodsItem = getItemObject(position);
        holder.mTvItemGoodsName.setText(goodsItem.getGoods_name());
        holder.mTvItemGoodsDesc.setText(goodsItem.getGoods_desc());
        holder.mTvItemGoodsPrice.setText(mCartFragment.getString(R.string.price_format, String.valueOf(goodsItem.getGoods_price())));
        Glide.with(mCartFragment)
                .load(HttpUtils.getHost() + goodsItem.getGoods_pic())
                .into(holder.mIvGoodsItemPic);
        holder.mSwipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
        holder.mSwipeLayout.addDrag(SwipeLayout.DragEdge.Left, holder.mFlSwipeDelete);
    }

    long getPriceSum() {
        long priceSum = 0;
        for (GoodsItem goodsItem : mData) {
            priceSum += goodsItem.getGoods_price();
        }
        return priceSum;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_item_goods_pic)
        ImageView mIvGoodsItemPic;
        @BindView(R.id.tv_item_goods_name)
        TextView mTvItemGoodsName;
        @BindView(R.id.tv_item_goods_price)
        TextView mTvItemGoodsPrice;
        @BindView(R.id.tv_item_goods_desc)
        TextView mTvItemGoodsDesc;
        @BindView(R.id.fl_swipe_delete)
        FrameLayout mFlSwipeDelete;
        @BindView(R.id.swipe_layout)
        SwipeLayout mSwipeLayout;
//        R.layout.item_goods_in_cart

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @OnClick(R.id.fl_swipe_delete)
        public void onClick() {
            mCartFragment.showDeleteConfirmDialog(getAdapterPosition());
        }
    }
}
